package com.cashmate.struts;

import com.cashmate.hibernate.dao.MatrixcriteriaDAO;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Matrixcriteria;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.ServletActionContext;

public class EditMatrixcriteria extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditMatrixcriteria.class);
    //This properties have setters below... The setters are called and populated by the grid.
    private String oper; //add,edit,del
    private String criterianame;
    private String id;
    private String totalvalue;
    private MatrixcriteriaDAO criter = (MatrixcriteriaDAO) ServletActionContext.getServletContext().getAttribute("matrixcriteriaDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    //Notice we don't have a success result because we do not need to redirect the user when he/she updates the dbase
    //We redirect him/her only if an error occurred.
    @Action(value = "/edit-matrixcriteria", results = {
        @Result(name = "invalid", location = "../error/formError.jsp"),
        @Result(name = "error", type = "redirectAction", location = "../error/messages.jsp")
    })
    @Override
    public String execute() throws Exception {
        Matrixcriteria matcrit;
        try {
            //oper is populated by the grid, the "add","edit","del" strings are constant values.
            if (oper.equalsIgnoreCase("add")) {
                matcrit = new Matrixcriteria(criterianame, Integer.parseInt(totalvalue), true);
                criter.insert(matcrit);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") added matrix criteria{criterianame:" + matcrit.getCriterianame() + ", totalvalue:" + matcrit.getTotalvalue() + "}");
            } else if (oper.equalsIgnoreCase("edit")) {
                matcrit = criter.get(Integer.parseInt(id));
                String old = matcrit.getCriterianame();
                matcrit.setCriterianame(criterianame);
                matcrit.setTotalvalue(Integer.parseInt(totalvalue));
                criter.update(matcrit);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated matrix criteria(" + old + ") with new values{criterianame:" + matcrit.getCriterianame() + ", totalvalue:" + matcrit.getTotalvalue() + "}");
            } else if (oper.equalsIgnoreCase("del")) {
                matcrit = criter.get(Integer.parseInt(id));
                matcrit.setIsactive(false);
                criter.update(matcrit);;
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") deleted matrix criteria(" + matcrit.getCriterianame() + ")");
            }
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
            }
            e.printStackTrace();
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }

    public void setCriterianame(String criterianame) {
        this.criterianame = criterianame;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }

    public void setTotalvalue(String totalvalue) {
        this.totalvalue = totalvalue;
    }
}
